// 执行代码的入口
const {version} = require("../package.json")
const program = require("commander");
const path = require("path");


// 循环配置命令
const mapActions = {
    create:{
        alias:"c",
        description:"create a project",
        examples:['uniapp-cli create <project-name>']
    },
    config:{
        alias:"conf",
        description:"config project variable",
        examples:["uniapp-cli config set <k><v>", "uniapp-cli config get <k>"]
    },
    "*":{
        alias:"",
        description:"command not found",
        examples:[]
    },
}

Reflect.ownKeys(mapActions).forEach((key)=>{
    const val = mapActions[key];
    program
    .command(key) //配置命令名称
    .alias(val.alias) //命令的别名
    .description(val.description) //命令的描述
    .option("-f, --force","overwrite target directory if it exists") //强制覆盖
    .action((option)=>{ //执行会触发的函数
        if(key==="*"){
            console.log(val.description)
        }else{
            //截取命令
            // console.log(key,process.argv)
            require(path.resolve(__dirname,key))(option,...process.argv.slice(3))
        }
    })
})

program.on("--help",()=>{ //显示命令和示例
    console.log("\nExamples:")
    Reflect.ownKeys(mapActions).forEach((key)=>{
        mapActions[key].examples.forEach(item=>{
            console.log(item)
        })
    })
})
// 添加 uniapp-cli -V获取版本号
program.version(version)

// 这行是上上面的这些生效
program.parse(process.argv)